﻿using System;

namespace SpatialHashLib.Topology.Indexing
{
    struct CellCoord : IEquatable<CellCoord>
    {
        public readonly int Row;
        public readonly int Col;

        public CellCoord(int row, int col)
        {
            Row = row;
            Col = col;
        }

        public Extent ToExtent(double cellSize)
        {
            Coord p0 = new Coord(Col * cellSize, Row * cellSize);
            Coord p1 = new Coord(p0.X + cellSize, p0.Y + cellSize);

            return new Extent(p0, p1);
        }

        public bool InCellExtent(CellExtent extent)
        {
            return
                Row >= extent.Min.Row && Row < extent.Max.Row &&
                Col >= extent.Min.Col && Col < extent.Max.Col;
        }

        public bool Equals(CellCoord other)
        {
            return Row == other.Row && Col == other.Col;
        }

        public override bool Equals(object obj)
        {
            if (obj is CellCoord)
                return Equals((CellCoord)obj);

            return false;
        }

        public override int GetHashCode()
        {
            return new HashFunction().Hash(Row).Hash(Col).HashCode;
        }

        public override string ToString()
        {
            return "(Row, Col: " + Row + ", " + Col + ")";
        }
    }
}
